package onlinejudge.service.impl;

import onlinejudge.dao.UserProblemDao;
import onlinejudge.entity.UserProblem;
import onlinejudge.service.ProblemService;
import onlinejudge.service.UserProblemService;
import onlinejudge.util.DateUtil;
import onlinejudge.vo.ProblemVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserProblemServiceImpl implements UserProblemService{

	@Resource
	private UserProblemDao userProblemDao;
	@Resource
	private ProblemService problemService;
	private Map<String, Object> parameters=new HashMap<String, Object>();
	
	@Override
	public List<ProblemVo> queryProblemByUserId(Map<String, Object> parameters1) {
		List<UserProblem> userProblemList=userProblemDao.queryUserProblem(parameters1);
		List<ProblemVo> problemList=problemService.queryAllProblem(parameters1);
		for (UserProblem userProblem : userProblemList) {
			for (ProblemVo problemVo : problemList) {
				problemVo.setSuccessCount(0);
				if(userProblem.getProblemId()==problemVo.getProblemId()) {
					problemVo.setStatus(userProblem.getStatus());
					problemVo.setSuccessCount(getSuccessCountByProblemId(problemVo.getProblemId()));
				}
			}
		}
		return problemList;
	}

	@Override
	public void addUserProblem(UserProblem userProblem) {
		parameters.clear();
		parameters.put("userId", userProblem.getUserId());
		parameters.put("problemId", userProblem.getProblemId());
		parameters.put("code",userProblem.getCode());
		parameters.put("status", userProblem.getStatus());
		parameters.put("submitDate", DateUtil.getDate());
		userProblemDao.addUserProblem(parameters);
	}

	@Override
	public void updateUserProblem(UserProblem userProblem) {
		parameters.clear();
		parameters.put("userId", userProblem.getUserId());
		parameters.put("problemId", userProblem.getProblemId());
		parameters.put("code",userProblem.getCode());
		parameters.put("status", userProblem.getStatus());
		parameters.put("submitDate", DateUtil.getDate());
		userProblemDao.updateUserProblem(parameters);
	}

	@Override
	public boolean judgeUserProblem(UserProblem userProblem) {
		parameters.clear();
		parameters.put("userId", userProblem.getUserId());
		parameters.put("problemId", userProblem.getProblemId());
		List<UserProblem> userProblemList=userProblemDao.queryUserProblem(parameters);
		if(userProblemList.size()==0) {
			//没做过
			return false;
		}
		return true;
	}

	@Override
	public List<ProblemVo> queryProblemByUserIdBySuccess(Map<String, Object> parameters1) {
		List<UserProblem> userProblemList=userProblemDao.queryUserProblem(parameters1);
		List<ProblemVo> problemList=new ArrayList<ProblemVo>();
		for (UserProblem userProblem : userProblemList) {
			if(userProblem.getStatus().equals("success")) {
				problemList.add(problemService.getProblem(userProblem.getProblemId()));
			}
		}
		return problemList;
	}

	@Override
	public Integer getSuccessCountByProblemId(Integer problemId) {
		parameters.clear();
		parameters.put("problemId", problemId);
		parameters.put("status", "success");
		return userProblemDao.queryUserProblem(parameters).size();
	}

	@Override
	public ProblemVo getSuccessProblem(Integer userId, Integer problemId) {
		parameters.clear();
		parameters.put("userId", userId);
		parameters.put("problemId", problemId);
		parameters.put("status", "success");
		UserProblem userProblem=userProblemDao.queryUserProblem(parameters).get(0);
		ProblemVo problem=problemService.getProblem(problemId);
		problem.setSuccessCode(userProblem.getCode());
		return problem;
	}

}
